package com.imagine.controller.client;

import com.imagine.entity.BaseResult;
import com.imagine.entity.LegalPersonalSecurityAccount;
import com.imagine.entity.PersonalSecurityAccount;
import com.imagine.service.LegalPersonalSecurityAccountService;
import com.imagine.service.PersonalSecurityAccountService;
import com.imagine.utils.TimeFormat;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;

/**
 * Created by liangyue on 2017/10/2.
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/securityAccount")
public class AccountController {

    @Autowired
    private PersonalSecurityAccountService personalSecurityAccountService;

    @Autowired
    private LegalPersonalSecurityAccountService legalPersonalSecurityAccountService;

    @ApiOperation(value="创建新的个人用户", notes="创建新的个人用户")
    @RequestMapping(value = "/p", method = RequestMethod.POST)
    public BaseResult insertPersonalSecurityAccount(
            @ApiParam("个人的姓名") @RequestParam(value = "pName") String pName,
            @ApiParam("本人性别") @RequestParam(value = "pSex") int pSex,
            @ApiParam("本人身份证号码") @RequestParam(value = "pIdNumber") String pIdNumber,
            @ApiParam("本人家庭地址") @RequestParam(value = "pHomeAddress") String pHomeAddress,
            @ApiParam("本人职业") @RequestParam(value = "pOccupation") String pOccupation,
            @ApiParam("本人学历") @RequestParam(value = "pEducation") String pEducation,
            @ApiParam("本人工作单位") @RequestParam(value = "pWorkUnit") String pWorkUnit,
            @ApiParam("本人联系电话") @RequestParam(value = "pPhoneNumber") String pPhoneNumber,
            @ApiParam("是否代办") @RequestParam(value = "pProxy") int pProxy,
            @ApiParam("代办人身份证号码") @RequestParam(value= "pProxyIdNumber") String pProxyIdNumber) throws IOException {
        PersonalSecurityAccount personalSecurityAccount = new PersonalSecurityAccount(
                new TimeFormat().todayDate(),
                pName,
                pSex,
                pIdNumber,
                pHomeAddress,
                pOccupation,
                pEducation,
                pWorkUnit,
                pPhoneNumber,
                pProxy,
                pProxyIdNumber,
                1
        );
        personalSecurityAccountService.insertPersonSecurityAccount(personalSecurityAccount);
        BaseResult result = new BaseResult();
        result.success(personalSecurityAccount.getpAccountId());
        result.setMessage("申请成功，等待审核");
        return result;
    }

    @ApiOperation(value="创建新的法人用户", notes="创建新的法人用户")
    @RequestMapping(value = "/l", method = RequestMethod.POST)
    public BaseResult insertLegalPersonalSecurityAccount(
            @ApiParam("法人注册登记号码") @RequestParam(value = "lAccountRegisterNumber") String lAccountRegisterNumber,
            @ApiParam("营业执照号码") @RequestParam(value = "lBusinessLicenserNumber") String lBusinessLicenserNumber,
            @ApiParam("法人的姓名") @RequestParam(value = "lName") String lName,
            @ApiParam("法人身份证号码") @RequestParam(value = "lIdNumber") String lIdNumber,
            @ApiParam("法人联系电话") @RequestParam(value = "lPhoneNumber") String lPhoneNumber,
            @ApiParam("法人联系地址") @RequestParam(value = "lContactAddress") String lContactAddress,
            @ApiParam("授权执行人姓名") @RequestParam(value = "authorizerName") String authorizerName,
            @ApiParam("授权人身份证号") @RequestParam(value = "authorizerIdNumber") String authorizerIdNumber,
            @ApiParam("授权人联系电话") @RequestParam(value = "authorizerPhoneNumber") String authorizerPhoneNumber,
            @ApiParam("授权人联系地址") @RequestParam(value= "authorizerContactAddress") String authorizerContactAddress) throws IOException {
        LegalPersonalSecurityAccount legalPersonalSecurityAccount = new LegalPersonalSecurityAccount(
                new TimeFormat().todayDate(),
                lAccountRegisterNumber,
                lBusinessLicenserNumber,
                lName,
                lIdNumber,
                lPhoneNumber,
                lContactAddress,
                authorizerName,
                authorizerIdNumber,
                authorizerPhoneNumber,
                authorizerContactAddress,
                1

        );
        legalPersonalSecurityAccountService.insertLegalPersonalSecurityAccount(legalPersonalSecurityAccount);
        BaseResult result = new BaseResult();
        result.success(legalPersonalSecurityAccount.getlAccountId());
        result.setMessage("申请成功，等待审核");
        return result;
    }

    @ResponseBody
    @ApiOperation(value="根据ID查询个人账户", notes="根据ID查询个人账户")
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public BaseResult getPersonalSecurityAccount(
            @ApiParam("账户类型") @RequestParam(value = "type") int type,
            @ApiParam("账户ID") @RequestParam(value = "id") int id) throws IOException {
        BaseResult result = new BaseResult();
        if (type == 0){
            if (personalSecurityAccountService.getPersonalSecurityAccountById(id).equals(null)){
                result.error("未开通");

            }
            else {
                result.success(personalSecurityAccountService.getPersonalSecurityAccountById(id));
            }
        }
        else {
            if (legalPersonalSecurityAccountService.getlAccountByLAccountId(id).equals(null)){
                result.error("未开通");

            }
            else {
                result.success(legalPersonalSecurityAccountService.getlAccountByLAccountId(id));
            }
        }

        return result;

    }
    @ResponseBody
    @ApiOperation(value="根据ID查询个人账户状态", notes="根据ID查询个人账户状态")
    @RequestMapping(value = "/state/id", method = RequestMethod.GET)
    public BaseResult getPersonalSecurityAccountState(
            @ApiParam("账户类型") @RequestParam(value = "type") int type,
            @ApiParam("账户ID") @RequestParam(value = "id") int id) throws IOException {
        BaseResult result = new BaseResult();
        if (type == 0){
            if (personalSecurityAccountService.getPersonalSecurityAccountById(id)==null){
                result.error("未申请");
                return result;
            }
            PersonalSecurityAccount account = personalSecurityAccountService.getPersonalSecurityAccountById(id);
            if (account.getpValidState() == 0){
                result.setMessage("账户无效");
            }
            else if (account.getpValidState() == 1){
                result.setMessage("账户正在审核中");
            }
            else {
                result.setMessage("账户审核成功，可以开设资金账户");
            }
            return result;
        }
        else {
            if (legalPersonalSecurityAccountService.getlAccountByLAccountId(id)==null){
                result.error("未申请");
                return result;
            }
            LegalPersonalSecurityAccount account = legalPersonalSecurityAccountService.getlAccountByLAccountId(id);
            if (account.getlValidState() == 0){
                result.setMessage("账户无效");
            }
            else if (account.getlValidState() == 1){
                result.setMessage("账户正在审核中");
            }
            else {
                result.setMessage("账户审核成功，可以开设资金账户");
            }
            return result;
        }


    }
    @ResponseBody
    @ApiOperation(value="挂失个人账户", notes="挂失个人账户")
    @RequestMapping(value = "/loss", method = RequestMethod.POST)
    public BaseResult getPersonalSecurityAccount(
            @ApiParam("账户类型") @RequestParam(value = "type") int type,
            @ApiParam("个人的姓名") @RequestParam(value = "name") String pName,
            @ApiParam("本人身份证号码") @RequestParam(value = "idNumber") String idNumber,
            @ApiParam("本人联系电话") @RequestParam(value = "pPhoneNumber") String phoneNumber

    ) throws IOException {
        if (type == 0){
            return personalSecurityAccountService.getPersonalSecurityAccountByPIdNumber(idNumber);
        }
        else return legalPersonalSecurityAccountService.getlAccountByLicense(idNumber);
    }


    @ResponseBody
    @ApiOperation(value="注销账户", notes="注销账户")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public BaseResult getPersonalSecurityAccount(
            @ApiParam("账户类型") @RequestParam(value = "type") int type,
            @ApiParam("本人身份证号码") @RequestParam(value = "idNumber") String idNumber,
            @ApiParam("本人联系电话") @RequestParam(value = "phoneNumber") String phoneNumber
    ) throws IOException {
        //如果用户是个人账户
        if (type == 0){
            return personalSecurityAccountService.updatepAccountStateByPIdNumber(idNumber, 0);
        }

        else return legalPersonalSecurityAccountService.updatelAccountStateBylIdNumber(idNumber, 0);
    }


}
